package com.chunuo.aclservice.config;

import com.chunuo.aclservice.filter.TokenAuthenticationFilter;
import com.chunuo.aclservice.filter.TokenLoginFilter;
import com.chunuo.aclservice.security.DefaultPasswordEncoder;
import com.chunuo.aclservice.security.TokenLogoutHandler;
import com.chunuo.aclservice.security.TokenManager;
import com.chunuo.aclservice.security.UnauthorizedEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;

/**
 * <p>
 * Security配置类
 * </p>
 *
 * @author qy
 * @since 2019-11-18
 */
//@Configuration
@EnableWebSecurity//里面包含了@Configuration注解
@EnableGlobalMethodSecurity(prePostEnabled = true)//判断用户对某个控制层的方法是否具有访问权限
public class TokenWebSecurityConfig extends WebSecurityConfigurerAdapter {

    private UserDetailsService userDetailsService;
    private DefaultPasswordEncoder defaultPasswordEncoder;
    private TokenManager tokenManager;
    private RedisTemplate redisTemplate;

    /**
     * 注入这些对象
     * @param userDetailsService
     * @param defaultPasswordEncoder
     * @param tokenManager
     * @param redisTemplate
     */
    @Autowired
    public TokenWebSecurityConfig(@Qualifier("userDetailsService") UserDetailsService userDetailsService,
                                  DefaultPasswordEncoder defaultPasswordEncoder,
                                  TokenManager tokenManager,
                                  RedisTemplate redisTemplate) {
        this.userDetailsService = userDetailsService;
        this.defaultPasswordEncoder = defaultPasswordEncoder;
        this.tokenManager = tokenManager;
        this.redisTemplate = redisTemplate;
    }

    /**
     * 配置设置
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.exceptionHandling()//添加异常处理支持
                //默认跳转到登录页面，但加上authenticationEntryPoint(new UnauthorizedEntryPoint())则可以返回自定义数据
                .authenticationEntryPoint(new UnauthorizedEntryPoint())
                .and().csrf().disable()//关闭csrf跨域攻击防御
                .authorizeRequests()//授权请求
                .anyRequest().authenticated()///所有请求都需要认证
                .and().logout().logoutUrl("/aclservice/admin/acl/index/logout")//注销的url
                .addLogoutHandler(new TokenLogoutHandler(tokenManager,redisTemplate))//注销执行该方法
                .and()
                .addFilter(new TokenLoginFilter(authenticationManager(), tokenManager, redisTemplate))//登录方法
                .addFilter(new TokenAuthenticationFilter(authenticationManager(), tokenManager, redisTemplate))
                .httpBasic();
    }

    /**
     * 密码处理
     * @param auth
     * @throws Exception
     */
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(defaultPasswordEncoder);
    }

    /**
     * 配置哪些请求不拦截
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/api/**",
                "/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**"
               );
    }
}